package charlie.shop.admin;

import charlie.shop.misc.Common;
import com.charlie.beans.Groups;
import com.charlie.beans.GroupsRoles;
import com.charlie.beans.Roles;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.GroupsFacadeLocal;
import com.charlie.sessions.GroupsRolesFacadeLocal;
import com.charlie.sessions.RolesFacadeLocal;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Edit group servlet
 *
 * @author Оленка
 */
@WebServlet(name = "EditGroups", urlPatterns = {"/admin/editGroup"})
public class EditGroups extends HttpServlet {

    @EJB
    private RolesFacadeLocal rolesFacade;
    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private GroupsRolesFacadeLocal groupsRolesFacade;
    @EJB
    private DAOLocal dao;
    Groups group;

    /**
     * Process groups and roles data
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Common.getParametrInt(request, "groupId");
        group = groupsFacade.findByGroupsId(Integer.valueOf(id));
        request.setAttribute("editGroup", group);
        request.setAttribute("rolesList", rolesFacade.findAll());
        List<String> groupRole = dao.getGroupRolesNamesList(id);
        request.setAttribute("groupRole", groupRole);       
        getServletContext().getRequestDispatcher("/admin/editGroup.jsp").forward(request, response);
    }

    /**
     * Remove role from group
     * 
     * @param groupId group to role romove
     * @param roleName role name
     */
    void removeGroupsRole(int groupId, String roleName) {
        dao.deleteGroupsRole(groupId, rolesFacade.findByRolesName(roleName).getRolesId());
    }

    /**
     * Edit group
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = (String) request.getParameter("groupName");

        int field_error = 0;

        if (name == null || name.length() == 0) {
            field_error = 1;
        }

        if (field_error > 0) {
            request.setAttribute("error_field", field_error);
            processRequest(request, response);
            return;
        }
        group.setName(name);
        groupsFacade.edit(group);

        //for checkboxes
        String select[] = request.getParameterValues("id");
        List<String> rolesName = dao.getGroupRolesNamesList(group.getGroupsId());
        if (select != null) {
            //check if some user role was uncheked
            boolean b;
            for (String r : rolesName) {
                b = false;
                for (String chr : select) {
                    if (r.equals(chr)) {
                        b = true;
                        break;
                    }
                }
                //if role was uncheked - delete it
                if (!b) {
                    removeGroupsRole(group.getGroupsId(), r);
                }
            }
            if (select.length != 0) {
                for (String roleName : select) {
                    //if user has not had cheked role yet
                    if (!rolesName.contains(roleName)) {
                        GroupsRoles gr = new GroupsRoles();
                        Roles role = rolesFacade.findByRolesName(roleName);
                        gr.setRolesId(role);
                        gr.setGroupsId(group);
                        groupsRolesFacade.create(gr);
                    }
                }
            }
        }
        getServletContext().getRequestDispatcher("/admin/completeEdition.jsp").forward(request, response);
    }
}
